package com.aistar.controller;

import com.aistar.pojo.Customer;
import com.aistar.pojo.Resume;
import com.aistar.service.ResumeService;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class ResumeController {

    @Autowired
    private ResumeService resumeService;
    @GetMapping("/resume/count")
    @ResponseBody
    public ServerResponse getcount(HttpServletRequest request){
        Customer customer = (Customer)request.getSession().getAttribute("loginCustomer");
        Integer cusId = customer.getCustId();
        System.out.println("登录用户id;"+cusId);

        ServerResponse serverResponse = resumeService.getCountByCustId(cusId);
        return serverResponse;
    }
//添加简历名称
    @PostMapping("/resume/name")
    @ResponseBody
    public ServerResponse add(String resumeName,HttpServletRequest request){
        //（1）添加新简历
        Customer customer = (Customer)request.getSession().getAttribute("loginCustomer");
        Integer custId = customer.getCustId();
        Resume resume = new Resume();
        resume.setCustId(custId);//用户id
        resume.setResumeName(resumeName);//简历名称
        resume.setResumeCreateTime(new Date());//简历创建时间
        resume.setStatus(0);//状态为0
        ServerResponse serverResponse = resumeService.add(resume);
        if (serverResponse.getStatus()==1){//添加失败
            serverResponse = resumeService.getLatestOne(custId);
        }
        return serverResponse;
    }
    //修改简历名称
    @PutMapping("/resume/name")
    @ResponseBody
    public ServerResponse update(Integer resumeId,String resumeName){
        Resume resume = new Resume();
        resume.setResumeId(resumeId);

        System.out.println(resumeId);

        resume.setResumeName(resumeName);
        resume.setResumeUpdateTime(new Date());

        ServerResponse serverResponse = resumeService.modify(resume);
        if (serverResponse.getStatus()==1){ //修改成功后，需要重新查询该resume (根据主键查询)
            serverResponse = resumeService.getById(resumeId); //此处的serverResponse 里面是 查询的家里对象resume
        }
       return serverResponse;
    }

//修改简历中用户信息
    @PutMapping("/resume/custInfo")
    @ResponseBody
    public ServerResponse updateCustInfo(Integer resumeId,String custName,String custGender,String custBirthday,
                                         String custStartWorkyear,Long custTelno,String custWorkStatus,String custEmail,
                                         String custLocation,String custPersonId){
        Date custbirthday = DateUtil.string2Date(custBirthday);

        //封装成Resume
        Resume resume = new Resume();
        resume.setResumeId(resumeId);
        resume.setOther1(custName);
        resume.setResumeGender(custGender);
        resume.setResumeBirth(custbirthday);
        resume.setOther2(custStartWorkyear);
        resume.setResumeTelno(custTelno);
        resume.setResumeEmail(custEmail);
        resume.setResumeLeavingWorking(custWorkStatus);
        resume.setResumeLiveCity(custLocation);
        resume.setResumePersonid(custPersonId);
        resume.setResumeUpdateTime(new Date());
        System.out.println(resume);
        //service
        ServerResponse serverResponse = resumeService.modify(resume);

        if (serverResponse.getStatus() == 1)
            serverResponse =  resumeService.getById(resumeId);
        return serverResponse;
    }


    //修改简历中年薪
    @PutMapping("/resume/yearsalary")
//    @RequestMapping(value="/resume/yearsalary",method=RequestMethod.PUT)
    @ResponseBody
    public ServerResponse updateYearSalary(Integer resumeId,Integer salary){

        //封装成Resume
        Resume resume = new Resume();
        resume.setResumeId(resumeId);
        resume.setResumeIncome(salary);
        resume.setResumeUpdateTime(new Date());

        //service
        ServerResponse serverResponse = resumeService.modify(resume);

        if (serverResponse.getStatus()==1)
            serverResponse = resumeService.getById(resumeId);
        return serverResponse;
    }


    //获得当前登录用户的所有简历（最多只有三个）
    @GetMapping("/resume")
    @ResponseBody
    public ModelAndView getAllResumeByCustId(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        Customer customer = (Customer)request.getSession().getAttribute("loginCustomer");
        Integer custId = customer.getCustId();
        ServerResponse serverResponse = resumeService.getAllByCustId(custId);
        List<Resume> resumeList =( List<Resume> ) serverResponse.getData();
        mav.addObject("resumeList",resumeList);
        mav.setViewName("/resume/resumeCenter");
        return mav;
    }
}
